﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;
using System.ComponentModel;

namespace salarioprofessores
{
    public partial class List : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {              
                bindGridView();
            }        
        }

        protected void RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowIndex != -1)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    object objTemp = GridView1.DataKeys[e.Row.RowIndex].Value as object;
                    if (objTemp != null)
                    {
                        string id = objTemp.ToString();

                        string sql1 = "";

                        sql1 = sql1 + "SELECT codigoSalarioProfessor, dataInicial, dataFinal, codigoProfessor, ValorSalario ";
                        //sql1 = sql1 + "(SELECT l.descricaoLocal ";
                        //sql1 = sql1 + "FROM local AS l INNER JOIN ";
                        //sql1 = sql1 + "trajeto ON l.codigoLocal = trajeto.codigoLocalSaida ";
                        //sql1 = sql1 + "WHERE (professor.codigoProfessor = salarioProfessor.codigoProfessor)) AS localSaida, ";
                        //sql1 = sql1 + "(SELECT l.descricaoLocal ";
                        //sql1 = sql1 + "FROM local AS l INNER JOIN ";
                        //sql1 = sql1 + "trajeto AS trajeto_1 ON l.codigoLocal = trajeto_1.codigoLocalChegada ";
                        //sql1 = sql1 + "WHERE (trajeto_1.codigoTrajeto = tarifaTrajeto.codigoTrajeto)) AS localChegada ";
                        sql1 = sql1 + "FROM salarioProfessor ";
                        sql1 = sql1 + "WHERE (codigoSalarioProfessor = " + id + ") AND (codigoProfessor = " + Request["codigoProfessor"].ToString() + ") ";
                        sql1 = sql1 + " order by convert(datetime,dataInicial,103), convert(datetime,dataFinal,103)";

                        System.Data.SqlClient.SqlConnection conexao = new System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings["baseDados"]);
                        conexao.Open();

                        System.Data.SqlClient.SqlCommand cmdT = new System.Data.SqlClient.SqlCommand(sql1, conexao);
                        System.Data.SqlClient.SqlDataReader drT = cmdT.ExecuteReader();

                        string codigoSalarioProfessor = "";
                        string dataInicial = "";
                        string dataFinal = "";
                        string codigoProfessor = "";
                        string ValorSalario = "";
                        //string localSaida = "";
                        //string localChegada = "";

                        int count = 0;

                        if (drT.HasRows)
                        {
                            while (drT.Read())
                            {
                                codigoSalarioProfessor = drT["codigoSalarioProfessor"].ToString();
                                dataInicial = drT["dataInicial"].ToString();
                                dataFinal = drT["dataFinal"].ToString();
                                codigoProfessor = drT["codigoProfessor"].ToString();
                                ValorSalario = drT["ValorSalario"].ToString();
                                //localSaida = drT["localSaida"].ToString();
                                //localChegada = drT["localChegada"].ToString();

                                if (count == 0)
                                {
                                    string literalccccccc = "<a href=" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/salarioProfessors/Edit.aspx?codigoSalarioProfessor=" + codigoSalarioProfessor + "&codigoProfessor=" + codigoProfessor + "&dataInicial=" + dataInicial + "&dataFinal=" + dataFinal + "&erro=ok><img src=" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/images/btEditar.gif alt=Editar /></a>";
                                    e.Row.Cells[4].Controls.Add(new LiteralControl(literalccccccc));

                                    string literalcccccc = "<a href=javascript:window.open('" + Convert.ToString(ConfigurationManager.AppSettings["url"]) + "/deletarsalarioProfessor.aspx?codigoSalarioProfessor=" + codigoSalarioProfessor + "&codigoProfessor=" + codigoProfessor + "','salario','location=0,resizable=0,status=0,scrollbars=0,width=350,height=150');>Excluir</a>";
                                    e.Row.Cells[4].Controls.Add(new LiteralControl(literalcccccc));


                                }
                                count++;
                            }
                        }
                        drT.Close();
                        cmdT.Dispose();
                        conexao.Close();
                    }
                }
            }
        }        

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            bindGridView();
        }

        public void bindGridView()
        {     
            string sql1 = "";

            sql1 = sql1 + "SELECT codigoSalarioProfessor, dataInicial, dataFinal, codigoProfessor, ValorSalario ";
            //sql1 = sql1 + "(SELECT l.descricaoLocal ";
            //sql1 = sql1 + "FROM local AS l INNER JOIN ";
            //sql1 = sql1 + "trajeto ON l.codigoLocal = trajeto.codigoLocalSaida ";
            //sql1 = sql1 + "WHERE (trajeto.codigoTrajeto = tarifaTrajeto.codigoTrajeto)) AS localSaida, ";
            //sql1 = sql1 + "(SELECT l.descricaoLocal ";
            //sql1 = sql1 + "FROM local AS l INNER JOIN ";
            //sql1 = sql1 + "trajeto AS trajeto_1 ON l.codigoLocal = trajeto_1.codigoLocalChegada ";
            //sql1 = sql1 + "WHERE (trajeto_1.codigoTrajeto = tarifaTrajeto.codigoTrajeto)) AS localChegada ";
            sql1 = sql1 + "FROM salarioProfessor WHERE (codigoProfessor = " + Request["codigoProfessor"].ToString() + ") ";
            sql1 = sql1 + " order by convert(datetime,dataInicial,103), convert(datetime,dataFinal,103)";

            string connString = ConfigurationManager.AppSettings["baseDados"];

            SqlDataAdapter dAdapter = new SqlDataAdapter(sql1, connString);

            SqlCommandBuilder cBuilder = new SqlCommandBuilder(dAdapter);

            DataTable dTable = new DataTable();

            dAdapter.Fill(dTable);

            GridView1.DataSource = dTable;

            dTable.Columns.Add("Açoes");

            GridView1.DataBind();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            bindGridView();
        }
    }
}
